import axios from 'axios'

// create an axios instance 创建axios实例
const service = axios.create({
  // 项目使用vue-cli创建的，基于webpack来进行搭建。使用npm run dev 运行项目，，通过node运行解析
  // process是node提供的全局对象
  // process.evn包含了用户环境信息的对象,是对项目的特殊设置
  // VUE_APP_BASE_API设置的环境变量
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

// request interceptor请求拦截器
service.interceptors.request.use(config => {
  // do something before request is sent

  return config
},
error => {
  // do something with request error
  return Promise.reject(error)
}
)

// response interceptor响应拦截器
service.interceptors.response.use(
  response => {
    return response
  },
  error => {
    return Promise.reject(error)
  }
)

export default service
